IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'up_Report_ProjectSummaryByProject')
	BEGIN
		DROP  Procedure  up_Report_ProjectSummaryByProject
	END

GO

CREATE Procedure up_Report_ProjectSummaryByProject
	@ProjectId int = null
AS

--HANDLE OLD VERSION
IF (@ProjectId IS NULL)
BEGIN
	SET @ProjectId = 6
END

DECLARE @totalOfCompleted int
DECLARE @totalOfEstimated int

SELECT 
	@totalOfCompleted = COUNT(*) 
FROM 
	tb_Vendor_InspVisit_XmissionStructures_Poles P
WHERE 
	P.Utility_Asset_XMissionStructures_PoleFk IS NOT NULL
	AND P.Vendor_ProjectFk = @projectId

SELECT 
	@totalOfEstimated = COUNT(*) 
FROM 
	tb_Utility_Asset_XMissionStructures_Poles a
INNER JOIN tb_utility_asset_xmissionstructures b
	ON a.utility_asset_xmissionstructurefk = b.utility_asset_xmissionstructureId

PRINT @totalOfEstimated
PRINT @totalOfCompleted

SELECT
	VP.*,
	dbo.fn_EstimateCompletionDate(@totalOfEstimated, @totalOfCompleted, VP.ActualStartDate) as estimateCompletionDate
FROM 
	tb_Vendor_Projects VP
WHERE 
	VP.Vendor_ProjectId = @ProjectId

GO

GRANT EXEC ON up_Report_ProjectSummaryByProject TO PPIReports

GO

